package com.chain.qs.common.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * Cache工具类
 */
public class EhCacheUtils {

	private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));

	private static final String SYS_CACHE = "sysCache";
	public static final String HANDOVER_CACHE = "handoverCache";
	public static final String HANDOVER_CUR_CACHE = "handoverCurCache";
	public static final String WARNING_RECORD_CACHE = "warningRecordCache";
	public static final String GRADE_FOUR_OFFICE_INFO_CACHE = "gradeFourOfficeInfoCache";//四级站点信息：id:name
	public static final String SGRP_INFO_CACHE = "sgrpInfoCache";//站组：id:name
	public static final String DEVICE_INFO_CACHE = "deviceInfoCache";//设备： id:name
	public static final String PARAM_ID_NAME_INFO_CACHE = "paramIdNameInfoCache";//设备参数： id:name
	public static final String PARAM_KID_ID_INFO_CACHE = "paramKidIdInfoCache";
	public static final String DEVICE_ID_TYPE_ID_INFO_CACHE = "deviceIdTypeIdInfoCache";//设备： id:设备类型Id
	public static final String DEVICE_ID_KEYWORD_INFO_CACHE = "deviceIdKeywordInfoCache";//设备： id:name
	public static final String PARAM_KID_EQUIP_ID_AND_ID_INFO_CACHE = "paramKidEquipIdAndIdInfoCache";//参数： kid+@+equip_id:id
	public static final String DEVICE_ID_EQUIP_KID_INFO_CACHE = "deviceIdEquipKidInfoCache";//设备： id:设备类型kid
	public static final String OFFICE_INFO_CACHE = "officeInfoCache";
	public static final String WARNING_RULE_MAP_INFO_CACHE = "warningRuleMapInfoCache";// code:name
	public static final String WARNING_RECORD_MAP_INFO_CACHE = "warningRecordMapInfoCache";// code:name
	public static final String NOT_RECOVERY_WARNING_RECORD_RULE_SET_IN_MAP_INFO_CACHE = "notRecoveryWarningRecordSetInfoCache";
	public static final String NOT_RECOVERY_WARNING_RECORD_RULE_MAP_INFO_CACHE = "notRecoveryWarningRecordMapInfoCache";//未恢复的告警信息


	/**
	 * 获取SYS_CACHE缓存
	 * @param key
	 * @return
	 */
	public static Object get(String key) {
		return get(SYS_CACHE, key);
	}

	/**
	 * 写入SYS_CACHE缓存
	 * @param key
	 * @return
	 */
	public static void put(String key, Object value) {
		put(SYS_CACHE, key, value);
	}

	/**
	 * 从SYS_CACHE缓存中移除
	 * @param key
	 * @return
	 */
	public static void remove(String key) {
		remove(SYS_CACHE, key);
	}

	/**
	 * 获取缓存
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public static Object get(String cacheName, String key) {
		Element element = getCache(cacheName).get(key);
		return element==null?null:element.getObjectValue();
	}

	/**
	 * 写入缓存
	 * @param cacheName
	 * @param key
	 * @param value
	 */
	public static void put(String cacheName, String key, Object value) {
		Element element = new Element(key, value);
		getCache(cacheName).put(element);
	}

	/**
	 * 从缓存中移除
	 * @param cacheName
	 * @param key
	 */
	public static void remove(String cacheName, String key) {
		getCache(cacheName).remove(key);
	}

	/**
	 * 获得一个Cache，没有则创建一个。
	 * @param cacheName
	 * @return
	 */
	private static Cache getCache(String cacheName){
		Cache cache = cacheManager.getCache(cacheName);
		if (cache == null){
			synchronized (cacheName){
				if (cache == null){
					cacheManager.addCache(cacheName);
					cache = cacheManager.getCache(cacheName);
					cache.getCacheConfiguration().setEternal(true);
				}
			}
		}
		return cache;
	}

	public static CacheManager getCacheManager() {
		return cacheManager;
	}

}
